package com.robot.member.service;

import com.robot.member.dao.AddressDao;
import com.robot.member.pojo.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * address服务层
 *
 * @author wuyunbin
 */
@Service
public class AddressService {
    @Autowired
    private AddressDao addressDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Address> findAll() {
        return addressDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Address> findSearch(Map whereMap, int page, int size) {
        Specification<Address> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return addressDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Address> findSearch(Map whereMap) {
        Specification<Address> specification = createSpecification(whereMap);
        return addressDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Address findById(String id) {
        return addressDao.findById(id).orElse(null);
    }

    /**
     * 增加
     *
     * @param address
     */
    public void add(Address address) {
        //雪花分布式ID生成器
        address.setId(idWorker.nextId() + "");
        LocalDateTime now = LocalDateTime.now();
        address.setCreatedAt(now);
        address.setUpdatedAt(now);
        addressDao.save(address);
    }

    /**
     * 修改
     *
     * @param address
     */
    public void update(Address address) {
        address.setUpdatedAt(LocalDateTime.now());
        addressDao.save(address);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        addressDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Address> createSpecification(Map searchMap) {
        return new Specification<Address>() {
            @Override
            public Predicate toPredicate(Root<Address> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 用户id
                String memberId = "memberId";
                if (searchMap.get(memberId) != null && !"".equals(searchMap.get(memberId))) {
                    predicateList.add(cb.like(root.get(memberId).as(String.class), "%" + (String) searchMap.get(memberId) + "%"));
                }
                // 收件人姓名
                String name = "name";
                if (searchMap.get(name) != null && !"".equals(searchMap.get(name))) {
                    predicateList.add(cb.like(root.get(name).as(String.class), "%" + (String) searchMap.get(name) + "%"));
                }
                // 电话
                String phone = "phone";
                if (searchMap.get(phone) != null && !"".equals(searchMap.get(phone))) {
                    predicateList.add(cb.like(root.get(phone).as(String.class), "%" + (String) searchMap.get(phone) + "%"));
                }
                // 性别 0-女 1-男 2-未知
                String sex = "sex";
                if (searchMap.get(sex) != null && !"".equals(searchMap.get(sex))) {
                    predicateList.add(cb.like(root.get(sex).as(String.class), "%" + (String) searchMap.get(sex) + "%"));
                }
                // 省份
                String province = "province";
                if (searchMap.get(province) != null && !"".equals(searchMap.get(province))) {
                    predicateList.add(cb.like(root.get(province).as(String.class), "%" + (String) searchMap.get(province) + "%"));
                }
                // 省份id
                String provinceId = "provinceId";
                if (searchMap.get(provinceId) != null && !"".equals(searchMap.get(provinceId))) {
                    predicateList.add(cb.like(root.get(provinceId).as(String.class), "%" + (String) searchMap.get(provinceId) + "%"));
                }
                // 城市
                String city = "city";
                if (searchMap.get(city) != null && !"".equals(searchMap.get(city))) {
                    predicateList.add(cb.like(root.get(city).as(String.class), "%" + (String) searchMap.get(city) + "%"));
                }
                // 城市id
                String cityId = "cityId";
                if (searchMap.get(cityId) != null && !"".equals(searchMap.get(cityId))) {
                    predicateList.add(cb.like(root.get(cityId).as(String.class), "%" + (String) searchMap.get(cityId) + "%"));
                }
                // 区县
                String area = "area";
                if (searchMap.get(area) != null && !"".equals(searchMap.get(area))) {
                    predicateList.add(cb.like(root.get(area).as(String.class), "%" + (String) searchMap.get(area) + "%"));
                }
                // 区县id
                String areaId = "areaId";
                if (searchMap.get(areaId) != null && !"".equals(searchMap.get(areaId))) {
                    predicateList.add(cb.like(root.get(areaId).as(String.class), "%" + (String) searchMap.get(areaId) + "%"));
                }
                // 详细地址
                String address = "address";
                if (searchMap.get(address) != null && !"".equals(searchMap.get(address))) {
                    predicateList.add(cb.like(root.get(address).as(String.class), "%" + (String) searchMap.get(address) + "%"));
                }
                // 经度
                String longitude = "longitude";
                if (searchMap.get(longitude) != null && !"".equals(searchMap.get(longitude))) {
                    predicateList.add(cb.like(root.get(longitude).as(String.class), "%" + (String) searchMap.get(longitude) + "%"));
                }
                // 纬度
                String latitude = "latitude";
                if (searchMap.get(latitude) != null && !"".equals(searchMap.get(latitude))) {
                    predicateList.add(cb.like(root.get(latitude).as(String.class), "%" + (String) searchMap.get(latitude) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 通过memberId查询地址列表并分页
     *
     * @param memberId
     * @param page
     * @param size
     * @return
     */
    public Page<Address> findAllByMemberIdAndDeletedAtIsNullOrderByCreatedAtDesc(String memberId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return addressDao.findAllByMemberIdAndDeletedAtIsNullOrderByCreatedAtDesc(memberId, pageable);
    }

    /**
     * 通过id和memberId查询地址
     *
     * @param id
     * @param memberId
     * @return
     */
    public Address findByIdAndMemberId(String id, String memberId) {
        return addressDao.findByIdAndMemberId(id, memberId);
    }
}
